﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Reflection;
using System.Threading;
using System.Data.SqlClient;

namespace ExBug
{
    public partial class ExBugSetupFrm : FrmCard
    {
        public ExBugSetupFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            workConnString = mInfo.workConnectionString;
            PACRTB.ButtonClick += new EventHandler(relationTextBox1_ButtonClick);
            ABRTB.ButtonClick += new EventHandler(relationTextBox1_ButtonClick);
            ALRTB.ButtonClick += new EventHandler(relationTextBox1_ButtonClick);
            OLPRTB.ButtonClick += new EventHandler(relationTextBox1_ButtonClick);
            CFPRTB.ButtonClick += new EventHandler(relationTextBox1_ButtonClick);
            eSRPDataSet.ExBugSetup.LunaCurentaColumn.DefaultValue = 1;
            eSRPDataSet.ExBugSetup.AnulCurentColumn.DefaultValue = DateTime.Now.Year;
            eSRPDataSet.ExBugSetup.PrimaryKeyColumn.DefaultValue = 0;
            eSRPDataSet.ExBugSetup.CodUnitateColumn.DefaultValue = mInfo.CodUnitate;
            eSRPDataSet.ExBugSetup.TiparesteContPeOrdonantareColumn.DefaultValue = false;
            eSRPDataSet.ExBugSetup.FolosesteDenumireBugetColumn.DefaultValue = false;
            eSRPDataSet.ExBugSetup.StergePACABGeneratColumn.DefaultValue = true;
            eSRPDataSet.ExBugSetup.ColoanaDepartamenteColumn.DefaultValue = true;
            eSRPDataSet.ExBugSetup.AfiseazaParteneriFolositiColumn.DefaultValue = false;
            eSRPDataSet.ExBugSetup.GenereazaNCOrdonantareColumn.DefaultValue = false;
        }

        void ExBugSetupFrm_HandleAfterAdd(object sender, DataGridViewCellEventArgs args)
        {
            
        }

        void relationTextBox1_ButtonClick(object sender, EventArgs e)
        {
            FrmMainMdi frm = ModuleInfo.mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;
            String numeClasa = "ERPForms.SeriiNumericeRezumatFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                     new object[] { ModuleInfo}, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;
                newFrm.SelectionAddOn = true;
                //newFrm.MdiParent = frm;
                if (newFrm.ShowDialog() == DialogResult.OK)
                {
                    //daca s-a selectat un rand se pune in text box valoare
                    ((RelationTextBox.RelationTextBox)sender).Text = newFrm.selectedRow.Cells["IdSerieColumn"].Value.ToString();
                }

            }
        }


        private void Inchidere_An(object sender, EventArgs e)
        {
            if (MessageBox.Show("Sunteti sigur ca doriti inchiderea anului curent?",
                            "Intrebare",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question) == DialogResult.No)
                return;
            if (MessageBox.Show("Documentele din anul curent se vor transfera in arhiva si vor fi vor putea fi doar consultate! Continuati?",
                            "Intrebare",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question) == DialogResult.No)
                return;
            SqlCommand cmd = null;
            MessageBox.Show("Atentie! Se vor inchide toate machetele. Salvati datele, acolo unde este cazul!",
                "Info",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            if (MdiParent != null)
                foreach (StudioForm fChild in MdiParent.MdiChildren)
                {
                    if (fChild != this)
                        fChild.Close();
                }

            try
            {
                Cursor.Current = Cursors.AppStarting;
                cmd = new SqlCommand("InchideAnExBug", DeschideConexiune(workConnString));
                cmd.CommandTimeout = 500;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("pAn", ((DataRowView)exBugSetupBindingSource.Current)["AnulCurent"].ToString()));
                cmd.Parameters.Add(new SqlParameter("codUnit", ModuleInfo.CodUnitate.ToString()));
                cmd.ExecuteNonQuery();

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                "Eroare",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
                return;
            }
            finally
            {
                InchideConexiune(cmd.Connection);
                Cursor.Current = Cursors.Default;
            }
            MessageBox.Show("Documentele curente au fost arhivate cu succes!",
                            "Info",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
        }

        private void ExBugSetupFrm_Load(object sender, EventArgs e)
        {
            this.exBugSetupTableAdapter.Connection = DeschideConexiune(workConnString);
            this.conturiTableAdapter.Connection = this.exBugSetupTableAdapter.Connection;
            this.conturiTableAdapter.Fill(this.eSRPDataSet.Conturi,ModuleInfo.CodUnitate);

            this.exBugSetupTableAdapter.Fill(this.eSRPDataSet.ExBugSetup,ModuleInfo.CodUnitate);
            InchideConexiune(this.exBugSetupTableAdapter.Connection);
            
            AddButton("Inchidere An", global::ERPStudio.Properties.Resources._001_41, Inchidere_An, ToolStripItemAlignment.Right, false);

            this.listBndSourceMain.Add(exBugSetupBindingSource);
            listTableAdaptor.Add(this.exBugSetupTableAdapter.Adapter);

            if (exBugSetupBindingSource.Count == 0)
            {
                Adauga();
                anCurentTB.Enabled = true;
                anCurentTB.ReadOnly = false;
            }
            else
            {
                exBugSetupBindingSource.AllowNew = false;
            }
            //numericUpDown1_ValueChanged(this, null);

        }

        /*private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            switch (Convert.ToInt16(numericUpDown1.Value))
            { 
                case 1:
                    labelLunaCurenta.Text = "Ianuarie";
                    break;
                case 2:
                    labelLunaCurenta.Text = "Februarie";
                    break;
                case 3:
                    labelLunaCurenta.Text = "Martie";
                    break;
                case 4:
                    labelLunaCurenta.Text = "Aprilie";
                    break;
                case 5:
                    labelLunaCurenta.Text = "Mai";
                    break;
                case 6:
                    labelLunaCurenta.Text = "Iunie";
                    break;
                case 7:
                    labelLunaCurenta.Text = "Iulie";
                    break;
                case 8:
                    labelLunaCurenta.Text = "August";
                    break;
                case 9:
                    labelLunaCurenta.Text = "Septembrie";
                    break;
                case 10:
                    labelLunaCurenta.Text = "Octombrie";
                    break;
                case 11:
                    labelLunaCurenta.Text = "Noiembrie";
                    break;
                case 12:
                    labelLunaCurenta.Text = "Decembrie";
                    break;




            }
        }*/

        private void button1_Click(object sender, EventArgs e)
        {
            if (openOPFV.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                caleOPFVTB.Text = openOPFV.FileName.Replace(openOPFV.SafeFileName,String.Empty);
            }
        }

        private void caleOPFVLabel_Click(object sender, EventArgs e)
        {

        }

        private void caleOPFVTB_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
